﻿using HCApp.UI.Windows.Views;
using SCDB.ViewModels;
using System.Collections.ObjectModel;
using System;
using System.Windows.Controls;
using System.Windows;
using MainAppEntryPrismWpf.Utilities;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using Syncfusion.Data.Extensions;
using Syncfusion.UI.Xaml.Grid;
using ImTools;

namespace MainAppEntryPrismWpf.Views
{
    /// <summary>
    /// Interaction logic for DailyRepository
    /// </summary>
    public partial class DailyRepository : ContentPageBase
    {

        public DataClassesSCDBDataContext SCDBDataContent;

        float zCL = 0;
        float zGZ = 0;

        //白班总产量和工资
        float zWhiteCL = 0;
        float zWhiteGZ = 0;

        //夜班总产量和工资
        float zNightCL = 0;
        float zNightGZ = 0;

        //白班平均产量和工资
        float averageWhiteCL = 0;
        float averageWhiteGZ = 0;

        //夜班平均产量和工资
        float averageNightCL = 0;
        float averageNightGZ = 0;



        float averageCL = 0;
        float averageGZ = 0;

        DateTime exportedDateTime;


        ObservableCollection<DansiDailySalary> dansiDailySalaries = new ObservableCollection<DansiDailySalary>();

        ObservableCollection<ZhiXianDailyOutputRepo> zhiXianDailyOutputRepo = new ObservableCollection<ZhiXianDailyOutputRepo>();

        ObservableCollection<ZhiXianDailyOutputRepo> zhiXianMultiOutputRepo = new ObservableCollection<ZhiXianDailyOutputRepo>();

        ObservableCollection<ZhixianDailyRepoItem> zhiXianDailyRepository = new ObservableCollection<ZhixianDailyRepoItem>();

        public DailyRepository()
        {
            InitializeComponent();

            SCDBDataContent = new DataClassesSCDBDataContext();
        }
        private void ContentPageBase_Loaded(object sender, RoutedEventArgs e)
        {

            //  getComDataTimer.Start();

        }

        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            DateTime certainDate = Convert.ToDateTime(sfDatePicker.Value?.ToString("yyyy-MM-dd"));


            if (certainDate > DateTime.Now)
            {
                MessageBox.Show("重新选择日期");
                return;
            }

            DateTime certainDateWhiteStart = certainDate.AddHours(7);
            DateTime certainDateWhiteEnd = certainDateWhiteStart.AddHours(12);

            DateTime certainDateNightStart = certainDate.AddHours(19);
            DateTime certainDateNightEnd = certainDateNightStart.AddHours(12);


            DateTime certainDateNext = certainDate.AddDays(1);

            exportedDateTime = certainDate;



            zhiXianDailyRepository.Clear();


            var beforeDataIncludeToday = (from data in SCDBDataContent.DSZX_CPCRKB
                                              //    where data.CJH == "制线"  && (data.RKSJ < certainDateWhiteStart || data.RKSJ >= certainDateNightEnd)
                                              //  where data.CJH == "制线" && (data.RKSJ <= certainDateWhiteStart)//certainDateWhiteEnd
                                              //    where data.CJH == "单丝" && (data.RKSJ <= certainDateWhiteEnd)//certainDateWhiteEnd
                                          where data.CJH == "单丝" && (data.RKSJ <= certainDateNightEnd)//certainDateWhiteEnd

                                          select data).ToList();


            var queryBeforeIncludeToday = from el in beforeDataIncludeToday.GroupBy(x => x.CPGG)
                              select new ZhixianDailyRepoItem
                              {
                                  CPGG = el.Key,
                                  //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                                  //  RKZL = (double)el.Sum(a => a.CKBZ == 1 ? -a.CPZL : a.CPZL),
                                  //  RKJS = (int)el.Sum(a => a.CKBZ == 1 ? -a.PS : a.PS),
                                  JCZL =Math.Round( (double)el.Sum(a => a.CKBZ == 0 || a.CKSJ > certainDateNightEnd ? a.CPZL : 0),3),
                                  JCJS = (int)el.Sum(a => a.CKBZ == 0 || a.CKSJ > certainDateNightEnd ? a.PS : 0),                                 

                              };
            var query3ListBeforeIncludeToday = queryBeforeIncludeToday.ToList().OrderBy(a => a.CPGG);


            var beforeDataBeforeToday = (from data in SCDBDataContent.DSZX_CPCRKB
                                              //    where data.CJH == "制线"  && (data.RKSJ < certainDateWhiteStart || data.RKSJ >= certainDateNightEnd)
                                              //  where data.CJH == "制线" && (data.RKSJ <= certainDateWhiteStart)//certainDateWhiteEnd
                                          where data.CJH == "单丝" && (data.RKSJ < certainDateWhiteStart)//certainDateWhiteEnd
                                          select data).ToList();


            var queryBeforeBeforeToday = from el in beforeDataBeforeToday.GroupBy(x => x.CPGG)
                                          select new ZhixianDailyRepoItem
                                          {
                                              CPGG = el.Key,
                                              //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                                              //  RKZL = (double)el.Sum(a => a.CKBZ == 1 ? -a.CPZL : a.CPZL),
                                              //  RKJS = (int)el.Sum(a => a.CKBZ == 1 ? -a.PS : a.PS),
                                              JCZL = (double)el.Sum(a => a.CKBZ == 0 ? a.CPZL : 0),
                                              JCJS = (int)el.Sum(a => a.CKBZ == 0 ? a.PS : 0),

                                          };
            var query3ListBeforeBeforeToday = queryBeforeBeforeToday.ToList().OrderBy(a => a.CPGG);




            var certainDayAll = (from data in SCDBDataContent.DSZX_CPCRKB
                                     //   where data.CJH == "制线" &&data.CKBZ==0 && data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd
                                     // where data.CJH == "制线" &&( (data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd)||(data.CKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd))
                                 where data.CJH == "单丝" && ((data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd) )
                                 select data).ToList();
            var queryDayAll = from el in certainDayAll.GroupBy(x => x.CPGG)
                           select new ZhixianDailyRepoItem
                           {
                               CPGG = el.Key,
                               //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                               RKZL =Math.Round( (double)el.Sum(a => a.CPZL),3),
                               RKJS = (int)el.Sum(a => a.PS),                              
                           //    CKZL = (double)el.Sum(a => a.CKBZ == 1 ? a.CPZL : 0),
                           //    CKJS = (int)el.Sum(a => a.CKBZ == 1 ? a.PS : 0),
                           };
            var query3ListAll = queryDayAll.ToList().OrderBy(a => a.CPGG);

            var certainDayOut = (from data in SCDBDataContent.DSZX_CPCRKB
                                     //   where data.CJH == "制线" &&data.CKBZ==0 && data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd
                                     // where data.CJH == "制线" &&( (data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd)||(data.CKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd))
                                 where data.CJH == "单丝" && ((data.CKSJ >= certainDateWhiteStart && data.CKSJ < certainDateNightEnd))
                                 select data).ToList();
            var queryDayOut = from el in certainDayOut.GroupBy(x => x.CPGG)
                              select new ZhixianDailyRepoItem
                              {
                                  CPGG = el.Key,                               
                               
                                  CKZL =Math.Round( (double)el.Sum(a => a.CKBZ == 1 ? a.CPZL : 0),3),
                                  CKJS = (int)el.Sum(a => a.CKBZ == 1 ? a.PS : 0),
                              };
            var query3ListOut = queryDayOut.ToList().OrderBy(a => a.CPGG);

            ZhixianDailyRepoItem x89;

          

            foreach (var item5 in query3ListBeforeIncludeToday)
            {
                x89 = new ZhixianDailyRepoItem();
                x89.CPGG= item5.CPGG;
                x89.JCJS= item5.JCJS;
                x89.JCZL =Math.Round( item5.JCZL,3);

                //

                //foreach (var item6 in query3ListBeforeBeforeToday)
                //{
                //    if (item6.CPGG==item5.CPGG)
                //    {
                //        x89.KCJS = item6.JCJS;
                //        x89.KCZL = item6.JCZL;
                //        break;
                //    }
                //}

                foreach (var item7 in query3ListAll)
                {
                    if (item7.CPGG == item5.CPGG)
                    {
                        x89.RKJS = item7.RKJS;
                        x89.RKZL = Math.Round(item7.RKZL,3);
                        break;
                    }
                }
                foreach (var item8 in query3ListOut)
                {
                    if (item8.CPGG == item5.CPGG)
                    {
                        x89.CKJS = item8.CKJS;
                        x89.CKZL =Math.Round( item8.CKZL,3);
                        break;
                    }
                }

                if (x89.CKJS==0&& x89.RKJS==0)
                {
                    x89.KCJS = x89.JCJS;
                    x89.KCZL = Math.Round(x89.JCZL,3);
                }
                else
                {
                    x89.KCJS = x89.JCJS + x89.CKJS - x89.RKJS; ;
                    x89.KCZL = Math.Round(x89.JCZL + x89.CKZL - x89.RKZL,3);
                 
                }

                zhiXianDailyRepository.Add(x89);

            }






            List<string> listGGby2 = (from data in SCDBDataContent.DSZX_CPGGB where data.CJH== "单丝" select data.CPGG).ToList();

            List<string> listBZby2 = listGGby2.OrderBy(s => s).ToList();

            ZhixianDailyRepoItem x82;

            foreach (var item in listBZby2)
            {
                x82 = new ZhixianDailyRepoItem();
                x82.CPGG = item;
                foreach (var item2 in query3ListBeforeIncludeToday)
                {
                    if (item == item2.CPGG)
                    {
                        x82.CPGG = item2.CPGG;                        
                        x82.JCZL = item2.JCZL;
                        x82.JCJS = item2.JCJS;


                        //foreach (var item3 in query3ListAll)
                        //{
                        //    if (item == item3.CPGG)
                        //    {
                        //        x82.CPGG = item3.CPGG;
                        //        x82.RKZL = item3.RKZL;
                        //        x82.RKJS = item3.RKJS;
                        //        x82.CKZL = item3.CKZL;
                        //        x82.CKJS = item3.CKJS;

                        //        x82.KCJS = x82.JCJS + x82.CKJS - x82.RKJS;
                        //        x82.KCZL = x82.JCZL + x82.CKZL - x82.RKZL;
                        //        break;
                        //    }
                        //    else
                        //    {
                        //        x82.KCJS = x82.JCJS ;
                        //        x82.KCZL = x82.JCZL ;
                        //    }
                        //}
                        break;
                    }
                }
                foreach (var item3 in query3ListAll)
                {
                    if (item == item3.CPGG) //找到匹配的数据
                    {
                        x82.CPGG = item3.CPGG;
                        x82.RKZL = item3.RKZL;
                        x82.RKJS = item3.RKJS;
                    //    x82.CKZL = item3.CKZL;
                    //    x82.CKJS = item3.CKJS;

                    //    x82.KCJS = x82.JCJS + x82.CKJS - x82.RKJS;
                    //    x82.KCZL = x82.JCZL + x82.CKZL - x82.RKZL;
                        break;
                    }
                }

                foreach (var item4 in query3ListOut)
                {
                    if (item == item4.CPGG) //找到匹配的数据
                    {
                        x82.CPGG = item4.CPGG;                    
                        x82.CKZL = item4.CKZL;
                        x82.CKJS = item4.CKJS;

                        x82.KCJS = x82.JCJS + x82.CKJS - x82.RKJS;
                        x82.KCZL = x82.JCZL + x82.CKZL - x82.RKZL;
                        break;
                    }
                }
                //找不到改怎样处理
                if (x82.KCJS==0&& x82.RKJS==0)
                {
                           x82.KCJS = x82.JCJS ;
                           x82.KCZL = x82.JCZL ;
                }
           //     zhiXianDailyRepository.Add(x82);
            
            }






            /*


            var beforeData = (from data in SCDBDataContent.DSZX_CPCRKB
                                  //    where data.CJH == "制线"  && (data.RKSJ < certainDateWhiteStart || data.RKSJ >= certainDateNightEnd)
                              where data.CJH == "制线" && (data.RKSJ < certainDateWhiteStart)
                              select data).ToList();


            var queryBefore = from el in beforeData.GroupBy(x => x.CPGG)
                           select new ZhixianDailyRepoItem
                           {
                               CPGG = el.Key,
                               //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                               RKZL = (double)el.Sum(a => a.CKBZ==1? -a.CPZL:a.CPZL),
                               RKJS = (int)el.Sum(a => a.CKBZ == 1 ? -a.PS:a.PS),

                           };
            var query3ListBefore = queryBefore.ToList().OrderBy(a => a.CPGG);
            
            var certainDayWhiteData = (from data in SCDBDataContent.DSZX_CPCRKB
                                           //   where data.CJH == "制线" &&data.CKBZ==0 && data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd
                                       where data.CJH == "制线"  && data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd
                                       select data).ToList();
            var queryDay = from el in certainDayWhiteData.GroupBy(x => x.CPGG)
                           select new ZhixianDailyRepoItem
                           {
                               CPGG = el.Key,
                               //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                               RKZL = (double)el.Sum(a => a.CPZL),
                               RKJS = (int)el.Sum(a => a.PS),
                               
                           };
            var query3List = queryDay.ToList().OrderBy(a => a.CPGG);


            var certainOutData = (from data in SCDBDataContent.DSZX_CPCRKB
                                       where data.CJH == "制线" && data.CKBZ == 1 && data.RKSJ >= certainDateWhiteStart && data.RKSJ < certainDateNightEnd
                                  select data).ToList();


            var queryOutputDay = from el in certainOutData.GroupBy(x => x.CPGG)
                           select new ZhixianDailyRepoItem
                           {
                               CPGG = el.Key,
                               //  YGBH = (el.Where(a => a.YGXM == el.Key).First()).YGBH.ToString(),
                               RKZL = (double)el.Sum(a => a.CPZL),
                               RKJS = (int)el.Sum(a => a.PS),

                           };

            var queryOutputList = queryOutputDay.ToList().OrderBy(a => a.CPGG);

            List<string> listGGby = (from data in SCDBDataContent.DSZX_CPGGB select data.CPGG ).ToList();

            List<string> listBZby = listGGby.OrderBy( s=>s).ToList();

            ZhixianDailyRepoItem x8;

            foreach (var item in listBZby)
            {
                x8 = new ZhixianDailyRepoItem();
                x8.CPGG = item;
                foreach (var item2 in query3List)
                {
                    if (item == item2.CPGG)
                    {
                        x8.CPGG = item2.CPGG;
                        x8.RKZL= item2.RKZL;
                        x8.RKJS = item2.RKJS;

                        break;
                    }
                }
                foreach (var item3 in queryOutputList)
                {
                    if (item == item3.CPGG)
                    {
                        x8.CPGG = item3.CPGG;
                        x8.CKZL = item3.RKZL;
                        x8.CKJS = item3.RKJS;
                        break;
                    }
                }
                foreach (var item4 in query3ListBefore)
                {
                    if (item == item4.CPGG)
                    {
                       // x8.CPGG = item3.CPGG;
                       x8.KCJS = item4.RKJS;
                       x8.KCZL = item4.RKZL;

                        x8.JCZL = x8.KCZL + x8.RKZL-x8.CKZL;
                        x8.JCJS = x8.KCJS +x8.RKJS-x8.CKJS;
                        break;
                    }
                }
                

                zhiXianDailyRepository.Add(x8);
            }
         
            */
            var filterCollection = new ObservableCollection<ZhixianDailyRepoItem>(zhiXianDailyRepository.Where(item => item.KCJS > 0 || item.CKJS >0  && item.RKJS > 0 ||item.JCJS > 0));

            zhiXianDailyRepository = filterCollection;


            datagridDaily.ItemsSource = zhiXianDailyRepository;
        }
        private void dailySalaryClick(object sender, RoutedEventArgs e)
        {
            string dllFilePath = Assembly.GetExecutingAssembly().Location;
            // string dllDirectoryPath = Path.GetDirectoryName(dllFilePath);        


            var filePath = @"d:\日库存\";
            DateTime certainDate = Convert.ToDateTime(sfDatePicker.Value?.ToString("yyyy-MM-dd"));
            var fileName = $"日库存{certainDate.ToString("yyyyMMddHHmmss")}.xlsx";
            var header = new List<ExcelHeader>() {

                          new ExcelHeader (){ FirstCol=0, LastCol=8, FirstRow=0, LastRow=0, Value="单丝生产日库存表" },//(第一行)(跨列0-6)
                          new ExcelHeader (){ FirstCol=3, LastCol=5, FirstRow=1, LastRow=1, Value="日期："+exportedDateTime.ToShortDateString() },//(第二行)(跨列0-6)
        
                           new ExcelHeader (){ FirstCol=0, LastCol=0, FirstRow=2, LastRow=2, Value="产品规格" },//第一列，跨两行(2-3)
		                   new ExcelHeader (){ FirstCol=1, LastCol=1, FirstRow=2, LastRow=2, Value="存件数" },//跨两列(1,2),同一行(第三行)
		                   new ExcelHeader (){ FirstCol=2, LastCol=2, FirstRow=2, LastRow=2, Value="存件重量" },
                           new ExcelHeader (){ FirstCol=3, LastCol=3, FirstRow=2, LastRow=2, Value="今日入库件数" },
                           new ExcelHeader (){ FirstCol=4, LastCol=4, FirstRow=2, LastRow=2, Value="今日入库重量" },
                            new ExcelHeader (){ FirstCol=5, LastCol=5, FirstRow=2, LastRow=2, Value="今日出库件数" },
                           new ExcelHeader (){ FirstCol=6, LastCol=6, FirstRow=2, LastRow=2, Value="今日出库重量" },
                            new ExcelHeader (){ FirstCol=7, LastCol=7, FirstRow=2, LastRow=2, Value="结余件数" },
                           new ExcelHeader (){ FirstCol=8, LastCol=8, FirstRow=2, LastRow=2, Value="结余重量" },


              

    };





            var data = new ExcelData<ZhixianDailyRepoItem> { StartCol = 0, StartRow = 3, Data = zhiXianDailyRepository };
            ExcelHelper.ExportExcel(filePath, fileName, header, data);

            MessageBox.Show(@"已成功导出到D:\日库存\！");

            //     datagridDaily.ItemsSource = dansiDailySalaries;


        }

    }
}
